package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShBackDao;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShBack;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShBackService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.RealUtil;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 银行卡(ShBack)表服务实现类
 *
 * @author makejava
 * @since 2020-09-05 14:15:32
 */
@Service("shBackService")
@Slf4j
@Transactional
public class ShBackServiceImpl implements ShBackService {
    @Resource
    private ShBackDao shBackDao;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShBack queryById(String id) {
        return this.shBackDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param shBack 实例对象
     * @return 实例对象
     */
    @Override
    public ShBack insert(ShBack shBack) {
        this.shBackDao.insert(shBack);
        return shBack;
    }

    /**
     * 修改数据
     *
     * @param shBack 实例对象
     * @return 实例对象
     */
    @Override
    public ShBack update(ShBack shBack) {
        this.shBackDao.update(shBack);
        return this.queryById(shBack.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.shBackDao.deleteById(id) > 0;
    }

    /**
     * 绑定银行卡
     * @param
     * @return
     */
    @Override
    public Result insertBack(HttpServletRequest request, String bankcard,String realName,String mobile,String idcard) {
        String name1 = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name1);
        if(shUser == null || name1 == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String json = RealUtil.market2(bankcard,realName,mobile,idcard);
        JSONObject jsonObject = JSONObject.fromObject(json).getJSONObject("data");
        log.info("银行卡四要素校验"+jsonObject);
        if(jsonObject.get("result").toString().equals("0")){
            JSONObject object = jsonObject.getJSONObject("bank_info");
            String  bank =object.get("bank").toString();
            System.out.println(bank);
            //自动生成id
            ShBack shBack = new ShBack();
            shBack.setId(UUIDGenerator.generate());
            shBack.setCardName(realName);
            shBack.setPhoneNum(mobile);
            shBack.setCardNum(bankcard);
            shBack.setOpenBank(bank);
            shBack.setUserId(shUser.getId());
            //通过userId和银行卡号查询实体信息
            ShBack back = shBackDao.selectBackByUserIdAndNum(shBack.getUserId(), shBack.getCardNum());
            if(back != null){
                return Result.error("您已经绑定的过该银行卡，不能重复绑定！");
            }
            shBackDao.insert(shBack);
            return Result.ok("绑定成功！");
        }
        return Result.error("校验结果不一致,请检查后重新绑定");
    }

    /**
     * 解除绑定银行卡
     * @param id
     * @return
     */
    @Override
    public Result deleteBackById(String id) {
        shBackDao.deleteById(id);
        return Result.ok("解除绑定成功！");
    }
}